<?php
class Admin_WorkerController extends Ctrl_Admin{
    public function indexAction(){
        $p = $_REQUEST;
        $pName = empty($p['name'])?'':Tool_Fnc::safe_string($p['name']);
        $pPhone = empty($p['phone'])?'':Tool_Fnc::safe_string($p['phone']);
        $pHiredate = empty($p['hiredate'])?'':Tool_Fnc::safe_string($p['hiredate']);
        $pDepartment = empty($p['department'])?'':Tool_Fnc::safe_string($p['department']);
        $pKeyword = empty($p['keyword'])?'':Tool_Fnc::safe_string($p['keyword']);

        $tWMO = new WorkerModel;
        $tSIMO = new SexinfoModel;
        $tNIMO = new NationinfoModel;
        $tDIMO = new DepartmentinfoModel;
        $tEIMO = new EducationlevelsinfoModel;
        $tRIMO = new RoleinfoModel;
        $tAMO = new AdminModel;
        
        $tWhere = ' 1 ';

        if(!empty($_COOKIE['admin']['roles'])){
            if($_COOKIE['admin']['roles'] != 1 && $_COOKIE['admin']['roles'] != 2){//管理员之外
                $tSql = 'select w.department department from '.$tAMO->table.' a left join '.$tWMO->table.' w on w.id = a.w_id where a.id = '.$_SESSION['admin']['id'];
                $tAData = $tAMO->query($tSql);
                $tWhere = ' 1 and w.department = '.$tAData[0]['department'];
            }
        }

        if(!empty($pKeyword)){
            $tWhere .= ' and w.name like \'%'.$pKeyword.'%\' or w.phone like \'%'.$pKeyword.'%\'';
        }
        if(!empty($pHiredate)){
            $tWhere .= ' and w.hiredate like \'%'.$pHiredate.'%\'';
        }
        if(!empty($pDepartment)){
            $tWhere .= ' and w.department = '.$pDepartment;
        }

        $tSql = 'select count(*) c from '.$tWMO->table.' w left join '.$tDIMO->table.' di on w.department = di.num where '.$tWhere;
        $tCount = $tWMO->query($tSql);
        $tCnt = empty($tCount[0]['c'])?0:$tCount[0]['c'];

        $tLimit = 5;
        $tPage = new Tool_Page($tCnt,$tLimit);
        if(!empty($tCnt)){
            $tSql = 'select w.native_place native_place,w.id id,w.name name,si.introduce sex,w.birthdate birthdate,w.phone phone,w.critcode critcode,ni.introduce nation,w.healthy_licence healthy_licence,di.introduce department,ri.introduce worker_type,w.hiredate hiredate,w.work_licence work_licence,ei.introduce education_levels,w.married married,w.native_place native_place from '.$tWMO->table.' w left join '.$tSIMO->table.' si on w.sex = si.num left join '.$tNIMO->table.' ni on w.nation = ni.num left join '.$tDIMO->table.' di on w.department = di.num left join '.$tEIMO->table.' ei on w.education_levels  = ei.num left join '.$tRIMO->table.' ri on ri.num = w.worker_type where '.$tWhere.' order by w.id desc limit '.$tPage->limit();
            $tDatas = $tWMO->query($tSql);
        }
        //所在部门显示
        $tDIMO = new DepartmentinfoModel;
        $tDIDatas = $tDIMO->field('num,introduce')->fList();
        $this->assign('pKeyword',$pKeyword);
        $this->assign('pHiredate',$pHiredate);
        $this->assign('pDepartment',$pDepartment);
        $this->assign('tDIDatas',$tDIDatas);
        $this->assign('tRIDatas',$tRIDatas);
        $this->assign('tShow',$tPage->show());
        $this->assign('tDatas',$tDatas);
    
    }
    public function addAction(){
        $p = $_REQUEST;
        
        $tDIMO = new DepartmentinfoModel;

        if($_SERVER['REQUEST_METHOD'] == 'POST'){
            if(!empty($p['role'])){
                if($p['role'] == 1 || $p['role'] == 2){
                    $tDIDatas = $tDIMO->field('num,introduce')->fList();
                    Tool_Fnc::ajaxMsg('',1,$tDIDatas);
                }else{
                    $tSql = 'select num,introduce from '.$tDIMO->table.' where num = '.$p['department_id'];
                    $tDIDatas = $tDIMO->query($tSql);
                    Tool_Fnc::ajaxMsg('',1,$tDIDatas);
                }
            }
            $pName = empty($p['senior_name'])?Tool_Fnc::ajaxMsg('姓名不能为空'):Tool_Fnc::safe_string($p['senior_name']);
            $pSex = empty($p['sex'])?'':Tool_Fnc::safe_string($p['sex']);
            $pAge = empty($p['age'])?Tool_Fnc::ajaxMsg('年龄不能为空'):Tool_Fnc::safe_string($p['age']);
            $pNation = empty($p['nation'])?'':Tool_Fnc::safe_string($p['nation']);
            $pReligion = empty($p['religion'])?'':Tool_Fnc::safe_string($p['religion']);
            $pHealthylicence = empty($p['healthy_licence'])?'':Tool_Fnc::safe_string($p['healthy_licence']);
            $pBirthdate = empty($p['birth_date'])?Tool_Fnc::ajaxMsg('出生年月不能为空'):Tool_Fnc::safe_string($p['birth_date']);
            $pCritcode = empty($p['critcode'])?Tool_Fnc::ajaxMsg('身份证号不能为空'):Tool_Fnc::safe_string($p['critcode']);
            $pNativeplace = empty($p['native_place'])?'':Tool_Fnc::safe_string($p['native_place']);
            $pMarried = empty($p['married'])?'':Tool_Fnc::safe_string($p['married']);
            $pWorklicence = empty($p['work_licence'])?'':Tool_Fnc::safe_string($p['work_licence']);
            $pPhone = empty($p['phone'])?'':Tool_Fnc::safe_string($p['phone']);
            $pHealthystatus = empty($p['healthy_status'])?'':Tool_Fnc::safe_string($p['healthy_status']);
            $pWorkertype = empty($p['worker_type'])?'':Tool_Fnc::safe_string($p['worker_type']);
            $pCreated = empty($p['created'])?'':Tool_Fnc::safe_string($p['created']);
            $pEducationlevels = empty($p['education_levels'])?'':Tool_Fnc::safe_string($p['education_levels']);
            $pResource = empty($p['resource'])?'':Tool_Fnc::safe_string($p['resource']);
            $pProbationperiod = empty($p['probation_period'])?'':Tool_Fnc::safe_string($p['probation_period']);
            $pProbationfee = empty($p['probation_fee'])?'':Tool_Fnc::safe_string($p['probation_fee']);
            $pFee = empty($p['fee'])?'':Tool_Fnc::safe_string($p['fee']); 
            $pAddress = empty($p['address'])?'':Tool_Fnc::safe_string($p['address']);
            $pRemarks = empty($p['remarks'])?'':Tool_Fnc::safe_string($p['remarks']);
            $pJobtype = empty($p['job_type'])?'':Tool_Fnc::safe_string($p['job_type']);
            $pDepartment = empty($p['department'])?'':Tool_Fnc::safe_string($p['department']);
            $pCases = empty($p['cases'])?'':implode(',',$p['cases']);
            $pSkill = empty($p['skill'])?'':implode(',',$p['skill']);
            $pDisease = empty($p['disease'])?'':implode(',',$p['disease']);
            //工作经历
            //$pWorktime = empty($p['worktime'])?'':$p['worktime'];
            $pDescriptions = empty($p['descriptions'])?'':$p['descriptions'];
            $pWorktype = empty($p['worktype'])?'':$p['worktype'];
            $pWorkplace = empty($p['workplace'])?'':$p['workplace'];
            $pWorkremarks = empty($p['workremarks'])?'':$p['workremarks'];
            $tWork = array(array());
            if(!empty($pWorkplace)){
            for($i=0;$i<count($pWorkplace);$i++){
                //$tWork[$i]['worktime'] = $pWorktime[$i];
                $tWork[$i]['descriptions'] = $pDescriptions[$i];
                $tWork[$i]['worktype'] = $pWorktype[$i];
                $tWork[$i]['workplace'] = $pWorkplace[$i];
                $tWork[$i]['workremarks'] = $pWorkremarks[$i];
            }
            }
            //家属信息
            $pJsname = empty($p['js_name'])?'':$p['js_name'];
            //$pJsage = empty($p['js_age'])?'':$p['js_age'];
            $pJssex = empty($p['js_sex'])?'':$p['js_sex'];
            $pJsrelation = empty($p['js_relation'])?'':$p['js_relation'];
            $pJscritcode = empty($p['js_critcode'])?'':$p['js_critcode'];
            $pJsphone = empty($p['js_phone'])?'':$p['js_phone'];            
            $pJsaddress = empty($p['js_address'])?'':$p['js_address'];
            $tJs = array(array());
            if(!empty($pJsname)){
            for($i=0;$i<count($pJsname);$i++){
                $tJs[$i]['js_name'] = $pJsname[$i];
                //$tJs[$i]['js_age'] = $pJsage[$i];
                $tJs[$i]['js_relation'] = $pJsrelation;
                $tJs[$i]['js_critcode'] = $pJscritcode[$i];
                $tJs[$i]['phone'] = $pJsphone[$i];
                $tJs[$i]['js_address'] = $pJsaddress[$i];
            }
            }
            //物品领取
            $pGoodsname = empty($p['goods_name'])?'':$p['goods_name'];
            $pGoodsstarttime = empty($p['goods_starttime']);
            $pGoodsendtime = empty($p['goods_endtime'])?'':$p['goods_endtime'];
            $pGoodsremarks = empty($p['goods_remarks']);
            $tGoods = array(array());
            if(!empty($pGoodsname)){
            for($i=0;$i<count($pGoodsname);$i++){
                $tGoods[$i]['goods_name'] = $pGoodsname[$i];
                $tGoods[$i]['goods_starttime'] = $pGoodsstarttime[$i];
                $tGoods[$i]['goods_endtime'] = $pGoodsendtime[$i];
                $tGoods[$i]['goods_remarks'] = $pGoodsremarks[$i];
            }
            }
            //账号添加
            $pDepartmentid = empty($p['department_id'])?'':Tool_Fnc::safe_string(rtrim($p['department_id'],','));
            $pAdminusername = empty($p['admin_username'])?'':Tool_Fnc::safe_string($p['admin_username']);
            $pAdminpasswd = empty($p['admin_passwd'])?'':trim($p['admin_passwd']);
            $pPrivateKey = Tool_Fnc::newrand();
            $pAdminpasswd = Tool_Fnc::markmd5($pAdminpasswd,$pPrivateKey);
            $tTime = time();

            if(!empty($_FILES['photo'])){
                $tImgurl = '/upload/worker/'.date('Y').'/'.date('m').'/'.date('d').'/';
                $tDir = APPLICATION_PATH . '/public' . $tImgurl;
                $tUpload = new Tool_Upload($_FILES['photo'],$tDir);
                $tSavename = $tUpload->getSaveName();
                $tExt = $tUpload->extension;
                $tFile = $tSavename . '.' . $tExt;
                $tRes = $tUpload->upload($tSavename);
                if($tRes == 1){
                    $tHeadimg = $tImgurl . $tFile;
                }
            }
            $tData = array(
                'name' => $pName,
                'photo' => $tHeadimg,
                'sex' => $pSex,
                'age' => $pAge,
                'nation' => $pNation,
                'religion' => $pReligion,
                'healthy_licence' => $pHealthylicence,
                'birthdate' => $pBirthdate,
                'critcode' => $pCritcode,
                'native_place' => $pNativeplace,
                'married' => $pMarried,
                'work_licence' => $pWorklicence,
                'phone' => $pPhone,
                'healthy_status' => $pHealthystatus,
                'worker_type' => $pWorkertype,
                'hiredate' => $pCreated,
                'education_levels' => $pEducationlevels,
                'resource' => $pResource,
                'probation_period' => $pProbationperiod,
                'probation_fee' => $pProbationfee,
                'fee' => $pFee,
                'old_address' => $pAddress,
                'remarks' => $pRemarks,
                'job_type' => $pJobtype,
                'department' => $pDepartment,
                'cases' => $pCases,
                'skill' => $pSkill,
                'disease' => $pDisease,
                'created' => $tTime,
            );

            $tWMO = new WorkerModel;
            if(!$tId = $tWMO->insert($tData)){
                Tool_Fnc::ajaxMsg('添加失败 001');
            }
            //工作经历
            $tWHMO = new WorkhistoryModel;
            if(!empty($tWork)){
                foreach($tWork as $tWrow){
                    if(!empty($tWrow['workplace'])){
                    $tData = array(
                        'worktime' => $tWrow['worktime'],
                        'descriptions' => $tWrow['descriptions'],
                        'work_type' => $tWrow['worktype'],
                        'work_place' => $tWrow['workplace'],
                        'remarks' => $tWrow['workremarks'],
                        'created' => $tTime,
                        'w_id' => $tId,
                    );
                    if(!$tWHMO->insert($tData)){
                        $tSMO->back();
                        Tool_Fnc::ajaxMsg('添加失败 004');
                    }
                    }
                }
            }
            //其他家属
            $tFMMO = new FamilymembersModel;

            if(empty(count($tJs))){Tool_Fnc::ajaxMsg('至少填写一位家属信息才可以提交');}

            if(!empty($tJs)){
                foreach($tJs as $tJsrow){
                    if(!empty($tJsrow['js_name'])){
                    $tData = array(
                        'name' => $tJsrow['js_name'],
            //            'age' => $tJsrow['js_age'],
                        'sex' => $tJsrow['js_sex'],
                        'relation' => $pJsrow['js_relation'],
                        'critcode' => $tJsrow['js_critcode'],
                        'phone' => $tJsrow['js_phone'],
                        'address' => $tJsrow['js_address'],
                        'is_client' => 2,
                        'created' => $tTime,
                        'w_id' => $tId,
                        'status' => 1,
                    );
                    if(!$tFMMO->insert($tData)){
                        $tSMO->back();
                        Tool_Fnc::ajaxMsg('添加失败 004');
                    }
                    }
                }
            }
            //物品领取
            $tGMO = new GoodsModel;
            if(!empty($tGoods)){
                foreach($tGoods as $tGoodsrow){
                    if(!empty($tGoodsrow['goods_name'])){
                    $tData = array(
                        'goodsname' => $tGoodsrow['goods_name'],
                        'starttime' => $tGoodsrow['goods_starttime'],
                        'endtime' => $tGoodsrow['goods_endtime'],
                        'remarks' => $tGoodsrow['goods_remarks'],
                        'created' => $tTime,
                        'w_id' => $tId,
                    );
                    if(!$tGMO->insert($tData)){
                        $tSMO->back();
                        Tool_Fnc::ajaxMsg('添加失败 004');
                    }
                    }
                }
            }
            //账号添加
            $tAMO = new AdminModel;
            if(!empty($pAdminusername) && !empty($pAdminpasswd)){
                $tData = array(
                    'w_id' => $tId,
                    'name' => $pAdminusername,
                    'passwd' => $pAdminpasswd,
                    'private_key' => $pPrivateKey,
                    'role' => $pWorkertype,
                    'department' => $pDepartmentid,
                    'status' => 1,
                    'created' => $tTime,
                    'user' => $_SESSION['admin']['name'],
                );
                if(!$tAMO->insert($tData)){
                    Tool_Fnc::ajaxMsg('添加失败');
                }
            }
            //扫描件
            if(!empty($_FILES['file_img'])){
                $tScMO = new ScanningModel;
                $fileimg = $_FILES['file_img'];
                $tImgurl = '/upload/scanning/'.date('Y').'/'.date('m').'/'.date('d').'/';
                $tDir = APPLICATION_PATH.'/public/upload/scanning/'.date('Y').'/'.date('m').'/'.date('d').'/';
                if($this->createDir($tDir)){
                    for($i=0;$i<count($fileimg['name']);$i++){
		                $tSaveName = $this->getSaveName();
                        if(strstr($fileimg['type'][$i],'image/jpeg')!==''){  
                            $tExt = 'jpg';  
                        }elseif(strstr($fileimg['type'][$i],'image/gif')!==''){  
                            $tExt ='gif';  
                        }elseif(strstr($fileimg['type'][$i],'image/png')!==''){  
 			                $tExt ='png';  
                        }else{
                            Tool_Fnc::ajaxMsg('非法文件');exit;
                        } 
                        $name = $fileimg['name'][$i];
                        $tImg = $tDir.$tSaveName.'.'.$tExt;
                        if(!move_uploaded_file($fileimg['tmp_name'][$i],iconv('UTF-8','gb2312',$tImg))){
                            Tool_Fnc::ajaxMsg('上传失败');exit;
                        };
                        //扫描件入库
                        $tData = array(
                            'w_id' => $tId,
                            'imgurl' => $tImgurl.$tSaveName.'.'.$tExt,
                            'created' => $tTime,
                        );
                        if(!$tScMO->insert($tData)){
                            Tool_Fnc::ajaxMsg('上传失败');exit;
                        }
                    }
                }
            }
            Tool_Fnc::ajaxMsg('添加成功',1);

        }
        //民族显示
        $tNIMO = new NationinfoModel;
        $tNIDatas = $tNIMO->field('num,introduce')->fList();
        //文化程度显示
        $tEIMO = new EducationlevelsinfoModel;
        $tEIDatas  = $tEIMO->field('num,introduce')->fList();
        //所在部门显示
        $tDIDatas = $tDIMO->field('num,introduce')->fList();
        #宗教显示
        $tReIMO = new ReligioninfoModel;
        $tReIDatas = $tReIMO->field('num,introduce')->fList();
        //人员类别显示
        $tRIMO = new RoleinfoModel;
        $tRIDatas = $tRIMO->field('num,introduce')->fList();

        $this->assign('tRIDatas',$tRIDatas);
        $this->assign('tReIDatas',$tReIDatas);
        $this->assign('tDIDatas',$tDIDatas);
        $this->assign('tEIDatas',$tEIDatas);
        $this->assign('tNIDatas',$tNIDatas);
    }
    public function editAction(){
        $p = $_REQUEST;
        $pId = empty($p['id'])?Tool_Fnc::ajaxMsg('ID不能为空'):Tool_Fnc::safe_string($p['id']);

        $tWMO = new WorkerModel;
        $tWHMO = new WorkhistoryModel;
        $tFMMO = new FamilymembersModel;
        $tGMO = new GoodsModel;
        $tAMO = new AdminModel;
        $tScMO = new ScanningModel;
        
        if($_SERVER['REQUEST_METHOD'] == 'POST'){
            $pId = empty($p['id'])?Tool_Fnc::ajaxMsg('id不能为空'):Tool_Fnc::safe_string($p['id']);
            $pName = empty($p['senior_name'])?Tool_Fnc::ajaxMsg('姓名不能为空'):Tool_Fnc::safe_string($p['senior_name']);
            $pSex = empty($p['sex'])?'':Tool_Fnc::safe_string($p['sex']);
            $pAge = empty($p['age'])?Tool_Fnc::ajaxMsg('年龄不能为空'):Tool_Fnc::safe_string($p['age']);
            $pNation = empty($p['nation'])?'':Tool_Fnc::safe_string($p['nation']);
            $pReligion = empty($p['religion'])?'':Tool_Fnc::safe_string($p['religion']);
            $pHealthylicence = empty($p['healthy_licence'])?'':Tool_Fnc::safe_string($p['healthy_licence']);
            $pBirthdate = empty($p['birth_date'])?Tool_Fnc::ajaxMsg('出生年月不能为空'):Tool_Fnc::safe_string($p['birth_date']);
            $pCritcode = empty($p['critcode'])?Tool_Fnc::ajaxMsg('身份证号不能为空'):Tool_Fnc::safe_string($p['critcode']);
            $pNativeplace = empty($p['native_place'])?'':Tool_Fnc::safe_string($p['native_place']);
            $pMarried = empty($p['married'])?'':Tool_Fnc::safe_string($p['married']);
            $pWorklicence = empty($p['work_licence'])?'':Tool_Fnc::safe_string($p['work_licence']);
            $pPhone = empty($p['phone'])?'':Tool_Fnc::safe_string($p['phone']);
            $pHealthystatus = empty($p['healthy_status'])?'':Tool_Fnc::safe_string($p['healthy_status']);
            $pWorkertype = empty($p['worker_type'])?'':Tool_Fnc::safe_string($p['worker_type']);
            $pCreated = empty($p['created'])?‘’:Tool_Fnc::safe_string($p['created']);
            $pEducationlevels = empty($p['education_levels'])?'':Tool_Fnc::safe_string($p['education_levels']);
            $pResource = empty($p['resource'])?'':Tool_Fnc::safe_string($p['resource']);
            $pProbationperiod = empty($p['probation_period'])?'':Tool_Fnc::safe_string($p['probation_period']);
            $pProbationfee = empty($p['probation_fee'])?'':Tool_Fnc::safe_string($p['probation_fee']);
            $pFee = empty($p['fee'])?'':Tool_Fnc::safe_string($p['fee']); 
            $pAddress = empty($p['address'])?'':Tool_Fnc::safe_string($p['address']);
            $pRemarks = empty($p['remarks'])?'':Tool_Fnc::safe_string($p['remarks']);
            $pJobtype = empty($p['job_type'])?'':Tool_Fnc::safe_string($p['job_type']);
            $pDepartment = empty($p['department'])?'':Tool_Fnc::safe_string($p['department']);
            $pCases = empty($p['cases'])?'':implode(',',$p['cases']);
            $pSkill = empty($p['skill'])?'':implode(',',$p['skill']);
            $pDisease = empty($p['disease'])?'':implode(',',$p['disease']);
            //工作经历
            //$pWorktime = empty($p['worktime'])?'':$p['worktime'];
            $pDescriptions = empty($p['descriptions'])?'':$p['descriptions'];
            $pWorktype = empty($p['worktype'])?'':$p['worktype'];
            $pWorkplace = empty($p['workplace'])?'':$p['workplace'];
            //$pWorkremarks = empty($p['workremarks'])?'':$p['workremarks'];
            $tWork = array(array());
            if(!empty($pWorkplace)){
            for($i=0;$i<count($pWorktime);$i++){
                //$tWork[$i]['worktime'] = $pWorktime[$i];
                $tWork[$i]['descriptions'] = $pDescriptions[$i];
                $tWork[$i]['worktype'] = $pWorktype[$i];
                $tWork[$i]['workplace'] = $pWorkplace[$i];
                //$tWork[$i]['workremarks'] = $pWorkremarks[$i];
            }
            }
            //家属信息
            $pJsname = empty($p['js_name'])?'':$p['js_name'];
            //$pJsage = empty($p['js_age'])?'':$p['js_age'];
            $pJssex = empty($p['js_sex'])?'':$p['js_sex'];
            $pJsrelation = empty($p['js_relation'])?'':$p['js_relation'];
            $pJscritcode = empty($p['js_critcode'])?'':$p['js_critcode'];
            $pJsphone = empty($p['js_phone'])?'':$p['js_phone'];            
            $pJsaddress = empty($p['js_address'])?'':$p['js_address'];
            $tJs = array(array());
            for($i=0;$i<count($pJsname);$i++){
                $tJs[$i]['js_name'] = $pJsname[$i];
                //$tJs[$i]['js_age'] = $pJsage[$i];
                $tJs[$i]['js_relation'] = $pJsrelation;
                $tJs[$i]['js_critcode'] = $pJscritcode[$i];
                $tJs[$i]['js_phone'] = $pJsphone[$i];
                $tJs[$i]['js_address'] = $pJsaddress[$i];
            }
            //物品领取
            $pGoodsname = empty($p['goods_name'])?'':$p['goods_name'];
            $pGoodsstarttime = empty($p['goods_starttime']);
            $pGoodsendtime = empty($p['goods_endtime'])?'':$p['goods_endtime'];
            $pGoodsremarks = empty($p['goods_remarks']);
            $tGoods = array(array());
            if(!empty($pGoodsname)){
            for($i=0;$i<count($pGoodsname);$i++){
                $tGoods[$i]['goods_name'] = $pGoodsname[$i];
                $tGoods[$i]['goods_starttime'] = $pGoodsstarttime[$i];
                $tGoods[$i]['goods_endtime'] = $pGoodsendtime[$i];
                $tGoods[$i]['goods_remarks'] = $pGoodsremarks[$i];
            }
            }
            //账号添加
            $pDepartmentid = empty($p['department_id'])?'':Tool_Fnc::safe_string(rtrim(',',$p['department_id']));
            $pAdminusername = empty($p['admin_username'])?'':Tool_Fnc::safe_string($p['admin_username']);
            $pAdminpasswd = empty($p['admin_passwd'])?'':trim($p['admin_passwd']);
            $pPrivateKey = Tool_Fnc::newrand();
            $pAdminpasswd = Tool_Fnc::markmd5($pAdminpasswd,$pPrivateKey);

            $tTime = time();
            if(!empty($_FILES['photo'])){
                $tImgurl = '/upload/worker/'.date('Y').'/'.date('m').'/'.date('d').'/';
                $tDir = APPLICATION_PATH . '/public' . $tImgurl;
                $tUpload = new Tool_Upload($_FILES['photo'],$tDir);
                $tSavename = $tUpload->getSaveName();
                $tExt = $tUpload->extension;
                $tFile = $tSavename . '.' . $tExt;
                $tRes = $tUpload->upload($tSavename);
                if($tRes == 1){
                    $tHeadimg = $tImgurl . $tFile;
                }
            }else{
                $tHeadimg = $p['photo'];
            }
            //基本信息添加
            $tData = array(
                'name' => $pName,
                'photo' => $tHeadimg,
                'sex' => $pSex,
                'age' => $pAge,
                'nation' => $pNation,
                'religion' => $pReligion,
                'healthy_licence' => $pHealthylicence,
                'birthdate' => $pBirthdate,
                'critcode' => $pCritcode,
                'native_place' => $pNativeplace,
                'married' => $pMarried,
                'work_licence' => $pWorklicence,
                'phone' => $pPhone,
                'healthy_status' => $pHealthystatus,
                'worker_type' => $pWorkertype,
                'hiredate' => $pCreated,
                'education_levels' => $pEducationlevels,
                'resource' => $pResource,
                'probation_period' => $pProbationperiod,
                'probation_fee' => $pProbationfee,
                'fee' => $pFee,
                'old_address' => $pAddress,
                'remarks' => $pRemarks,
                'job_type' => $pJobtype,
                'department' => $pDepartment,
                'cases' => $pCases,
                'skill' => $pSkill,
                'disease' => $pDisease,
                'updated' => $tTime,
                'id' => $pId,
            );
            if(!$tWMO->update($tData)){
                Tool_Fnc::ajaxMsg('修改失败');
            }
            //工作经历
            if(!empty($tWork)){
                foreach($tWork as $tWrow){
                    $tWHData = $tWHMO->field('id')->where('worktime = \''.$tWrow['worktime'].'\''.' and descriptions = \''.$tWrow['descriptions'].'\''.' and worktype = \''.$tWrow['worktype'].'\''.' and workplace = \''.$tWrow['workplace'].'\''.' and w_id = '.$pId)->fRow();
                    if(!empty($tWHData['id'])){
                    $tData = array(
                        'worktime' => $tWrow['worktime'],
                        'descriptions' => $tWrow['descriptions'],
                        'work_type' => $tWrow['worktype'],
                        'work_place' => $pWrow['workplace'],
                        'remarks' => $tWrow['workremarks'],
                        'updated' => $tTime,
                        'w_id' => $pId,
                        'id' => $tWHData['id'],
                    );
                    if(!$tWHMO->update($tData)){
                        $tSMO->back();
                        Tool_Fnc::ajaxMsg('修改失败 004');
                    }
                    }else{
                    if(!empty($tWrow['worktime'])){
                    $tData = array(
                        'worktime' => $tWrow['worktime'],
                        'descriptions' => $tWrow['descriptions'],
                        'work_type' => $tWrow['worktype'],
                        'work_place' => $pWrow['workplace'],
                        'remarks' => $tWrow['workremarks'],
                        'created' => $tTime,
                        'w_id' => $pId,
                    );
                    if(!$tWHMO->insert($tData)){
                        $tSMO->back();
                        Tool_Fnc::ajaxMsg('修改失败 004');
                    }
                    }
                    }
                }
            }
            //其他家属
            if(!empty($tJs)){
                foreach($tJs as $tJsrow){
                    $tFMData = $tFMMO->field('id')->where('critcode = \''.$tJsrow['js_critcode'].' and w_id = '.$pId)->fRow();
                    if(!empty($tFMData['id'])){
                    $tData = array(
                        'name' => $tJsrow['js_name'],
                        //'age' => $tJsrow['js_age'],
                        'sex' => $tJsrow['js_sex'],
                        'relation' => $pJsrow['js_relation'],
                        'critcode' => $tJsrow['js_critcode'],
                        'phone' => $tJsrow['js_phone'],
                        'address' => $tJsrow['js_address'],
                        'is_client' => 2,
                        'updated' => $tTime,
                        'w_id' => $pId,
                        'id' => $tFMData['id'],
                    );
                    if(!$tFMMO->update($tData)){
                        $tSMO->back();
                        Tool_Fnc::ajaxMsg('修改失败 005');
                    }
                    }else{
                    if(!empty($tJsrow['js_name'])){
                    $tData = array(
                        'name' => $tJsrow['js_name'],
                        //'age' => $tJsrow['js_age'],
                        'sex' => $tJsrow['js_sex'],
                        'relation' => $pJsrow['js_relation'],
                        'critcode' => $tJsrow['js_critcode'],
                        'phone' => $tJsrow['js_phone'],
                        'address' => $tJsrow['js_address'],
                        'is_client' => 2,
                        'created' => $tTime,
                        'w_id' => $pId,
                    );
                    if(!$tFMMO->insert($tData)){
                        $tSMO->back();
                        Tool_Fnc::ajaxMsg('修改失败 005');
                    }
                    }
                    }
                }
            }
            //物品领取
            if(!empty($tGoods)){
                foreach($tGoods as $tGoodsrow){
                    $tGData = $tGMO->field('id')->where('goodsname = \''.$tGoodsrow['goods_name'].'\''.' and starttime = \''.$tGoodsrow['goods_starttime'].'\''.' and endtime = \''.$tGoodsrow['goods_endtime'].'\''.' and remarks = \''.$tGoodsrow['goods_remarks'].'\''.' and w_id = '.$pId)->fRow();
                    if(!empty($tGData['id'])){
                    $tData = array(
                        'goodsname' => $tGoodsrow['goods_name'],
                        'starttime' => $tGoodsrow['goods_starttime'],
                        'endtime' => $tGoodsrow['goods_endtime'],
                        'remarks' => $tGoodsrow['goods_remarks'],
                        'created' => $tTime,
                        'w_id' => $pId,
                        'id' => $tGData['id'],
                    );
                    if(!$tGMO->update($tData)){
                        $tSMO->back();
                        Tool_Fnc::ajaxMsg('修改失败 006');
                    }
                    }else{
                    if(!empty($tGoodsrow['goods_name'])){
                    $tData = array(
                        'goodsname' => $tGoodsrow['goods_name'],
                        'starttime' => $tGoodsrow['goods_starttime'],
                        'endtime' => $tGoodsrow['goods_endtime'],
                        'remarks' => $tGoodsrow['goods_remarks'],
                        'created' => $tTime,
                        'w_id' => $pId,
                    );
                    if(!$tGMO->insert($tData)){
                        $tSMO->back();
                        Tool_Fnc::ajaxMsg('修改失败 006');
                    }
                    }
                    }
                }
            }
            //账号添加
            if(!empty($pAdminusername) && !empty($pAdminpasswd)){
                $tAData = $tAMO->field('id')->where('w_id = '.$pId)->fRow();
                if(!empty($tAData['id'])){
                $tData = array(
                    'w_id' => $pId,
                    'name' => $pAdminusername,
                    'passwd' => $pPasswd,
                    'private_key' => $pPrivateKey,
                    'role' => $pWorkertype,
                    'role_status' => 1,
                    'department' => $pAdmindepartment,
                    'status' => 1,
                    'created' => $tTime,
                    'user' => $_SESSION['admin']['name'],
                    'id' => $tAData['id'],
                );
                if(!$tAMO->update($tData)){
                    Tool_Fnc::ajaxMsg('修改失败 007');
                }
                }else{
                $tData = array(
                    'w_id' => $pId,
                    'name' => $pAdminusername,
                    'passwd' => $pPasswd,
                    'private_key' => $pPrivateKey,
                    'role' => $pWorkertype,
                    'role_status' => 1,
                    'department' => $pAdmindepartment,
                    'status' => 1,
                    'created' => $tTime,
                    'user' => $_SESSION['admin']['name'],
                );
                if(!$tAMO->insert($tData)){
                    Tool_Fnc::ajaxMsg('修改失败 007');
                }
                
                }
            }
            //扫描件
            if(!empty($_FILES['file_img'])){

                $fileimg = $_FILES['file_img'];
                $tImgurl = '/upload/scanning/'.date('Y').'/'.date('m').'/'.date('d').'/';
                $tDir = APPLICATION_PATH.'/public/upload/scanning/'.date('Y').'/'.date('m').'/'.date('d').'/';
                if($this->createDir($tDir)){
                    for($i=0;$i<count($fileimg['name']);$i++){
		                $tSaveName = $this->getSaveName();
                        if(strstr($fileimg['type'][$i],'image/jpeg')!==''){  
                            $tExt = 'jpg';  
                        }elseif(strstr($fileimg['type'][$i],'image/gif')!==''){  
                            $tExt ='gif';  
                        }elseif(strstr($fileimg['type'][$i],'image/png')!==''){  
 			                $tExt ='png';  
                        }else{
                            Tool_Fnc::ajaxMsg('非法文件');exit;
                        } 
                        $name = $fileimg['name'][$i];
                        $tImg = $tDir.$tSaveName.'.'.$tExt;
                        if(!move_uploaded_file($fileimg['tmp_name'][$i],iconv('UTF-8','gb2312',$tImg))){
                            Tool_Fnc::ajaxMsg('上传失败');exit;
                        };
                        //扫描件入库
                        $tData = array(
                            'w_id' => $pId,
                            'imgurl' => $tImgurl.$tSaveName.'.'.$tExt,
                            'created' => $tTime,
                        );
                        if(!$tScMO->insert($tData)){
                            Tool_Fnc::ajaxMsg('上传失败');exit;
                        }
                    }
                }
            }
            Tool_Fnc::ajaxMsg('修改成功',1);

        }
        //编辑页面显示
        $tWData = $tWMO->field('*')->where('id = '.$pId)->fRow();
        //民族显示
        $tNIMO = new NationinfoModel;
        $tNIDatas = $tNIMO->field('num,introduce')->fList();
        //宗教显示
        $tReIMO = new ReligioninfoModel;
        $tReIDatas = $tReIMO->field('num,introduce')->fList();
        //文化程度显示
        $tEIMO = new EducationlevelsinfoModel;
        $tEIDatas  = $tEIMO->field('num,introduce')->fList();
        //所在部门显示
        $tDIMO = new DepartmentinfoModel;
        $tDIDatas = $tDIMO->field('num,introduce')->fList();
        //人员类别显示
        $tRIMO = new RoleinfoModel;
        $tRIDatas = $tRIMO->field('num,introduce')->fList();
        //工作经历
        $tWHDatas = $tWHMO->field('*')->where('w_id = '.$pId)->fList();
        //家属信息
        $tFDatas = $tFMMO->field('*')->where('w_id = '.$pId)->fList();
        
        //物品信息
        $tGDatas = $tGMO->field('*')->where('w_id = '.$pId)->fList();
        //账号权限显示
        $tADatas = $tAMO->field('passwd,department,name')->where('w_id = '.$pId)->fRow();
        //扫描件显示
        $tScDatas = $tScMO->field('id,imgurl')->where('w_id = '.$pId)->fList();

        $this->assign('tADatas',$tADatas);
        $this->assign('tGDatas',$tGDatas);
        $this->assign('tFDatas',$tFDatas);
        $this->assign('tWHDatas',$tWHDatas);
        $this->assign('pId',$pId);
        $this->assign('tWData',$tWData);
        $this->assign('tDIDatas',$tDIDatas);
        $this->assign('tRIDatas',$tRIDatas);
        $this->assign('tReIDatas',$tReIDatas);
        $this->assign('tEIDatas',$tEIDatas);
        $this->assign('tNIDatas',$tNIDatas);
        $this->assign('tScDatas',$tScDatas);
    }
    public function deleteAction(){
        $p = $_REQUEST;
        $pId = empty($p['id'])?'':Tool_Fnc::safe_string($p['id']);
        $pWhID = empty($p['wh_id'])?'':Tool_Fnc::safe_string($p['wh_id']);
        $pJsID = empty($p['js_id'])?'':Tool_Fnc::safe_string($p['js_id']);
        $pWpID = empty($p['wp_id'])?'':Tool_Fnc::safe_string($p['wp_id']);
        $pScID = empty($p['sc_id'])?'':Tool_Fnc::safe_string($p['sc_id']);
        #工作经历删除
        if(!empty($pWhID)){
            $tMO = new WorkhistoryModel;
            if(!$tMO->del($pWhID)){
                Tool_Fnc::ajaxMsg('删除失败');
            }
            Tool_Fnc::ajaxMsg('删除成功',1);
        }
        #家属信息删除
        if(!empty($pJsID)){
            $tMO = new FamilymembersModel;
            if(!$tMO->del($pJsID)){
                Tool_Fnc::ajaxMsg('删除失败');
            }
            Tool_Fnc::ajaxMsg('删除成功',1);
        }
        #物品信息删除
        if(!empty($pWpID)){
            $tMO = new GoodsModel;
            if(!$tMO->del($pWpID)){
                Tool_Fnc::ajaxMsg('删除失败');
            }
            Tool_Fnc::ajaxMsg('删除成功',1);
        }
        #员工删除
        if(!empty($pId)){
            $tMO = new WorkerModel;
            if(!$tMO->del($pWhID)){
                Tool_Fnc::ajaxMsg('删除失败');
            }
            Tool_Fnc::ajaxMsg('删除成功',1);
        }
        #扫描件删除
        if(!empty($pScID)){
            $tScMO = new ScanningModel;
            $tRow = $tScMO->field('imgurl')->where('id = '.$pScID)->fRow();
            if(!$tScMO->del($pScID)){
                Tool_Fnc::ajaxMsg('删除失败');
            }
            @unlink(APPLICATION_PATH.'/public'.$tRow['imgurl']);
            Tool_Fnc::ajaxMsg('删除成功',1);
        }
    }
	public function createDir($folder){
		$reval = false;
		if(!file_exists($folder)){
			@umask(0);
			preg_match_all('/([^\/]*)\/?/i', $folder, $atmp);
			$base = ($atmp[0][0] == '/') ? '/' : '';
			
			foreach($atmp[1] AS $val) {
				if('' != $val){
					$base .= $val;
					if ('..' == $val || '.' == $val) {
						$base .= '/';
						continue;
					}
				}else{
					continue;
				}

				$base .= '/';

				if(!file_exists($base)){
					if(@mkdir($base, 0777)){
						@chmod($base, 0777);
						$reval = true;
					}
				}
			}
		}else{
			$reval = is_dir($folder);
		}
		
		clearstatcache();
		return $reval;
	}
	public function getSaveName(){
		$newName = md5(md5(date('YmdHis') . rand(1000, 99999)).'pyp123');

		return $newName;
	}
}
?>
